<template>
  <div>
    <el-form :inline="true">
      <el-row>
        <el-col :span="6">
          <el-form-item label="行业：">
            <el-input
              v-model="selectData.industryName"
              placeholder="请输入行业"
              style="width: 240px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="请选择状态：">
            <el-select
              v-model="selectData.industryStatus"
              @change="handleStatusChange"
              style="width: 200px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="getIndustryHandler">查询</el-button>
          <el-button type="primary" @click="resetHandler">重置</el-button>
          <el-button type="primary" @click="openAddDialog">添加岗位</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="industryName" label="行业"> </el-table-column>
      <el-table-column prop="industryStatus" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.industryStatus == 0" style="color: green"
            >启用</span
          >
          <span v-else-if="scope.row.industryStatus == 1" style="color: red"
            >禁用</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="operation" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="openUpdateDialog(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.industryStatus == 0"
            size="mini"
            type="danger"
            @click="updateIndustryStatusHandler(scope.row)"
            >禁用</el-button
          >
          <el-button
            v-if="scope.row.industryStatus == 1"
            size="mini"
            type="primary"
            @click="updateIndustryStatusHandler(scope.row)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog title="新增行业" width="30%" :visible.sync="addDialogVisible">
      <el-form>
        <el-form-item label="行业名称：">
          <el-input v-model="addData.industryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addIndustryHandler">提交</el-button>
        <el-button @click="cancelAddIndustryHanlder">取消</el-button>
      </div>
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog
      title="修改行业信息"
      width="30%"
      :visible.sync="updateDialogVisible"
    >
      <el-form>
        <el-form-item label="行业名称：">
          <el-input v-model="updateData.industryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateIndustryHandler"
          >提交</el-button
        >
        <el-button @click="cancelUpdateIndustryHanlder">取消</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="selectData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="selectData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getIndustry, updateIndustrye, addIndustry } from "@/api/industry";
export default {
  data() {
    return {
      options: [
        { value: "", label: "全部" },
        { value: 0, label: "启用" },
        { value: 1, label: "禁用" },
      ],
      addData: {
        industryName: null,
      },
      selectData: {
        industryName: null,
        industryStatus: "",
        currentPage: 1, //当前页
        pageSize: 5, //每页大小
      },
      total: 0,
      updateData: {
        industryId: null,
        industryName: null,
        industryStatus: "",
      },
      tableData: [],
      addDialogVisible: false,
      updateDialogVisible: false,
    };
  },
  methods: {
    //查询不同状态的公司
    handleStatusChange() {
      this.selectData.currentPage = 1;
    },
    //取消新增行业按钮
    cancelAddIndustryHanlder() {
      this.addDialogVisible = false;
    },

    //新增行业
    addIndustryHandler() {
      this.$confirm(`此操作将新增行业, 是否继续?`, "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          addIndustry(this.addData).then((res) => {
            this.addDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "新增成功",
              });

              this.getIndustryHandler();
            }
          });
        })
        .catch(() => {});
    },

    //打开新增弹窗的方法
    openAddDialog() {
      this.addDialogVisible = true;
    },
    //修改行业状态
    updateIndustryStatusHandler(row) {
      this.$confirm(
        `此操作将修改行业名为${row.industryName}的行业状态, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定修改",
          cancelButtonText: "取消修改",
        }
      ).then(() => {
        const statusData = {
          industryId: row.industryId,
          industryStatus: row.industryStatus === 0 ? 1 : 0,
        };
        updateIndustrye(statusData).then((res) => {
          this.updateDialogVisible = false;
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "修改成功",
            });
            this.getIndustryHandler();
          }
        });
      });
    },

    //取消修改行业按钮
    cancelUpdateIndustryHanlder() {
      this.updateDialogVisible = false;
    },
    //提交修改行业信息
    updateIndustryHandler() {
      this.$confirm(
        `此操作将修改行业名为${this.updateData.industryName}的行业信息, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定修改",
          cancelButtonText: "取消修改",
        }
      )
        .then(() => {
          updateIndustrye(this.updateData).then((res) => {
            this.updateDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getIndustryHandler();
            }
          });
        })
        .catch(() => {});
    },
    //打开修改弹窗的方法
    openUpdateDialog(row) {
      this.updateDialogVisible = true;
      this.updateData.industryName = row.industryName;
      this.updateData.industryId = row.industryId;
    },
    //重置
    resetHandler() {
      this.selectData.industryName = null;
      this.selectData.industryStatus = null;
      this.getIndustryHandler();
    },
    //查询
    getIndustryHandler() {
      getIndustry(this.selectData)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
        .catch(() => {});
    },

    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.getIndustryHandler();
    },
    handleCurrentChange(val) {
      this.selectData.currentPage = val;
      this.getIndustryHandler();
    },
  },
  mounted() {
    this.getIndustryHandler();
  },
};
</script>

<style>
</style>